package com.logistic.server.controller;

import com.logistic.server.common.annotation.LoginRequired;
import com.logistic.server.entity.common.LoginBody;
import com.logistic.server.entity.common.Result;
import com.logistic.server.entity.common.ResultGenerator;
import com.logistic.server.entity.common.WebUserToken;
import com.logistic.server.service.ISysLoginService;
import com.logistic.server.service.ISysPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * 登录控制器
 *
 * @author swen
 */
@Api(value = "登录控制器", tags = {"登录控制器"})
@RestController
public class SysLoginController {

    @Resource
    private ISysLoginService loginService;

    @Resource
    private ISysPermissionService permissionService;

    /**
     * 用户登录
     * @param loginBody 用户名 密码
     * @return 结果
     */
    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result<?> login(@RequestBody LoginBody loginBody) {
        return ResultGenerator.genSuccessResult(loginService.login(loginBody));
    }

    /**
     * 获取用户信息
     * @param webUserToken 用户信息
     * @return 结果
     */
    @ApiOperation("获取用户信息")
    @LoginRequired
    @GetMapping("/getInfo")
    public Result<?> getInfo(WebUserToken webUserToken) {
        return ResultGenerator.genSuccessResult(new HashMap<String, Object>(){{
            put("user", webUserToken);
            put("roles", permissionService.getRolePermission(webUserToken.getUserId()));
            put("permissions", permissionService.getMenuPermission(webUserToken.getUserId()));
        }});
    }

    /**
     * 退出登陆
     * @param webUserToken 用户信息
     * @return 结果
     */
    @ApiOperation("退出登陆")
    @GetMapping("/logout")
    public Result<?> logout(WebUserToken webUserToken) {
        loginService.Logout(webUserToken);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 获取路由信息
     * @return 路由信息
     */
    @ApiOperation("获取路由信息")
    @LoginRequired
    @GetMapping("getRouters")
    public Result<?> getRouters(WebUserToken webUserToken) {
        return ResultGenerator.genSuccessResult(loginService.getRouters(webUserToken));
    }
}
